const URL = "https://souti.yuncoding.cn:8000/api/";
// const token = "1302be2558703b5bd5e1e557ce4d06c5347b7a2c";
var token = "";

export function on_login(code, encryptedData, iv) {
    return new Promise(function (resolve, reject) {
        wx.request({
            url: URL + 'account/on_login/',
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            method: "POST",
            data: {
                code: code,
                encryptedData,
                encryptedData,
                iv: iv
            },
            success: function (res) {
                var app = getApp();
                app.globalData.university = res.data.university;
                app.globalData.phone = res.data.phone
                token = res.data.token;
                resolve(token);
            },
            fail: function (res) {
                reject(res);
            }
        })
    })

}

export function getMyBooks() {
    return new Promise(function (resolve, reject) {
        wx: wx.request({
            url: URL + 'account/my_books/',
            header: {
                Authorization: "Token " + token
            },
            method: 'GET',
            success: function (res) {
                resolve(res.data)
            },
            fail: function (res) {
                reject(res)
            }
        })
    })
}

export function SearchBook(keyValue, cb) {
    wx: wx.request({
        url: URL + 'book/books/?',
        data: {
            key: keyValue
        },
        header: {
            Authorization: "Token " + token
        },
        method: 'GET',
        success: function (res) {
            cb(res.data)
        }
    })
}

//获取图书章节
export function getChapter(bookId, cb) {
    wx: wx.request({
        url: URL + 'book/chapter/?',
        header: {
            Authorization: "Token " + token
        },
        data: {
            book_id: bookId
        },
        method: "GET",
        success: function (res) {
            cb(res.data)
        }
    })
}

export function changeMyBook(bookIds, status, cb) {
    wx: wx.request({
        url: URL + 'account/change_my_book/?',
        method: "POST",
        header: {
            "Authorization": "Token " + token,
            "content-type": "application/x-www-form-urlencoded"
        },
        data: {
            book_ids: JSON.stringify(bookIds),
            status: status
        },
        success: function (res) {
            // console.log(res)
            cb(res.data);
        }
    })
}

export function getTi(chapterId, tiNumber, cb) {
    wx: wx.request({
        url: URL + 'book/ti/?',
        method: "GET",
        data: {
            chapter_id: chapterId,
            ti_number: tiNumber,
        },
        header: {
            Authorization: "Token " + token,
        },
        success: function (res) {
            cb(res)
        }
    })
}
//提建议
export function suggest(adviceType, content, cb) {
    wx: wx.request({
        url: URL + 'account/advice/',
        data: {
            advice_type: adviceType,
            content: content
        },
        header: {
            "Authorization": "Token " + token,
        },
        method: 'POST',
        success: function (res) {
            // console.log(res)
            console.log('发送成功')
            if (cb != '' && typeof cb == 'function') {
                cb(res)
            }
        }
    })
}
//错误反馈
export function errFeedBack(tiId, content, cb) {
    wx: wx.request({
        url: URL + 'account/advice/',
        data: {
            advice_type: '1',
            ti_id: tiId,
            content: content
        },
        header: {
            "Authorization": "Token " + token,
        },
        method: 'POST',
        success: function (res) {
            // console.log(res)
            console.log('发送成功')
            if (cb != '' && typeof cb == 'function') {
                cb(res)
            }
        }
    })
}

export function completeInfo(university, phone, cb) {
    wx.request({
        url: URL + 'account/',
        data: {
            university: university,
            phone: phone
        },
        header: {
            "Authorization": "Token " + token,
        },
        method: 'POST',
        success: function (res) {
            cb(res)
        }
    })
}
export function popSearches(cb) {
    wx.request({
        url: URL + 'book/hotest/',
        header: {
            "Authorization": "Token " + token,
        },
        method: 'GET',
        success: function (res) { cb(res) },
    })
}